public class TestSingleList {

    static class Node {
        public int val;
        public Node next;

        public Node(int val){
            this.val = val;
        }
    }

    public Node head;//表示用来代表头节点的引用。

    /**
     * 先创建一个链表（为了便于理解）
     */
    public void createList(){
        Node node1 = new Node(10);
        Node node2 = new Node(12);
        Node node3 = new Node(23);
        Node node4 = new Node(34);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    //打印顺序表
    public void Display(){
        Node cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    //时间复杂度为O(1)。
    public void addFirst(int data){
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

    //尾插法
    //时间复杂度为O(n)。
    public void addLast(int data){
        Node node = new Node(data);
        if(head == null){
            head = node;
        }else{
            Node cur = head;
            while(cur.next!=null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    //任意位置插入
    private void checkIndex(int index){
        if(index<0||index>Size()){
            throw new IndexNotLegalException("index位置不合法！");
        }
    }
    public void addIndex(int index,int data){
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == Size()){
            addLast(data);
        }
        //说明不是头插不是尾插且index合法
        Node node = new Node(data);
        Node cur = findIndexSubOfOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    //找到要插入位置index的前一个
    public Node findIndexSubOfOne(int index){
        Node cur = head;
        while(index-1!=0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public Node findPrevOfKey(int key){
        Node cur = head;
        while(cur.next != null){
            if(cur.next.val==key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现的key。
    public void remove(int key){
        //若要删除的key为头结点
        if(head.val == key){
            head = head.next;
            return;
        }
        Node cur = findPrevOfKey(key);
        if(cur==null) return;
        cur.next = cur.next.next;
    }

    //删除所有的key。
    public void allKeyRemove(int key){
        if(head == null) return;
        Node cur = head.next;
        Node prev = head;
        while(cur!=null){
            if(cur.val==key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }

    //判断链表中是否有key。
    public boolean Contains(int key){
        Node cur = head;
        while(cur!=null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public int Size(){
        Node cur = head;
        int count = 0;
        while(cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public Node reverseList(){
        if(head==null) return null;
        if(head.next==null) return head;
        Node cur = head.next;
        head.next = null;
        while(cur!=null){
            Node curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //清空链表
    public void clear(){
        //this.head = null;
        Node cur = head;
        Node curNext = cur.next;
        while(cur!=null){
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

    public Node middleList () {
        Node fast = head;
        Node slow = head;
        if(head==null) return null;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public Node findKthTOFail(int k){
        if(head==null||k<0) return null;
        Node fast = head;
        Node slow = head;
        for (int i = 1; i < k; i++) {
            fast = fast.next;
            if(fast==null) return null;
        }
        while(fast.next!=null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
